import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_business_core/business_customer/less_components/business_customer_details_dynamic_list.dart';
import 'package:x3_component_core/x3_component_core.dart';

class BusinessCustomerDetailsCustomerDynamic extends StatefulWidget {
  BusinessCustomerDetailsCustomerDynamic({Key key, @required this.vipId}) : super(key: key);

  final String vipId;

  @override
  _BusinessCustomerDetailsCustomerDynamicState createState() => _BusinessCustomerDetailsCustomerDynamicState();
}

class _BusinessCustomerDetailsCustomerDynamicState extends State<BusinessCustomerDetailsCustomerDynamic> with AutomaticKeepAliveClientMixin {
  BaseRefreshController _refreshController;

  ///过滤动态
  final List<String> _filterDynamics = ["全部","分享","浏览","收藏","取消收藏","点赞","评论","关注公众号","取消公众号"];
  String _select = '-2';

  @override
  void initState() {
    _refreshController = BaseRefreshController(initParams: {'vipId': widget.vipId, 'interactType': '-2'});
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BusinessCustomerDetailsDynamicList(
      header: _buildFilterCardWidget(),
      service: '/vip/customer/query/customernews',
      controller: _refreshController,
      decorator: (child) {
        return BaseCardComponent(child: child);
      },
    );
  }

  Widget _buildFilterCardWidget() {
    return BaseCardComponent(
      padding: EdgeInsets.fromLTRB(12, 12, 12, 0),
      child: BaseScreenRefreshComponent(
        request: BaseScreenRequestModel('/vip/customer/query/interact/types', method: BaseHttpMethodEnum.GET),
        builder: (BaseHttpResultModel result) {
          List<dynamic> _filterItem = result.data;
          _filterItem.removeWhere((element) {
            String value = BaseUtils.getKeyData(element, 'value').toString();
            int _index = _filterDynamics.indexWhere((indexElement) => indexElement == value);
            return _index == -1;
          });
          return BaseWrapExpandDown(data: _filterItem, builder: _buildFilterItem);
        },
      ),
    );
  }

  Widget _buildFilterItem(dynamic item) {
    String key = BaseUtils.getKeyData(item, 'key').toString();
    String value = BaseUtils.getKeyData(item, 'value').toString();
    Color activeColor = key == this._select ? Theme.of(context).textTheme.bodyText2.color : Colors.white;
    return InkWell(
      borderRadius: BorderRadius.circular(4.0),
      onTap: () {
        if (this._select != key) {
          setState(() {
            this._select = key;
          });
          _refreshController.refresh(params: {'interactType': key}, isSearch: true);
        }
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 12.0),
        decoration: BoxDecoration(
          border: Border.all(width: 0.8, color: activeColor),
          borderRadius: BorderRadius.circular(4.0),
        ),
        child: Text(value),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
